package com.ceo.common.config;

import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

/**
 * @author uto_dev_008
 * @desc
 * @since 2021/11/22 14:49
 * Copyright (c)  All rights reserved
 */
@Configuration
@EnableSwagger2
@EnableKnife4j
public class SwaggerConfig {
    @Bean
    public Docket createRestApi() {
        //添加head参数配置start
        ParameterBuilder tokenPar = new ParameterBuilder();
        ParameterBuilder langPar = new ParameterBuilder();
        ParameterBuilder sidPar = new ParameterBuilder();
        List<Parameter> pars = new ArrayList<Parameter>();
        tokenPar.name("Authorization").description("令牌").modelRef(new ModelRef("string")).defaultValue("eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJjbGllbnQiOiJQQyIsImV4cCI6MTYzNzk4NDc0MCwidXNlcmlkIjoiYWRtaW4iLCJ1c2VybmFtZSI6IjEyMyJ9.RxxqBNlgeGyJu0RxhjXEWU-U2Vt_Swd699BoX1KFrA8").parameterType("header").required(true);
        langPar.name("lanaguage").description("语言").modelRef(new ModelRef("String")).parameterType("header").required(false);
        sidPar.name("sid").description("日志跟踪id").modelRef(new ModelRef("String")).parameterType("header").required(false);
        pars.add(tokenPar.build());
        pars.add(langPar.build());
        pars.add(sidPar.build());


        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.ceo"))
                .paths(PathSelectors.any())
                .build()
                .globalOperationParameters(pars);//注意这里
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("yang")
                .description("powered by By-Health")
                .termsOfServiceUrl("")
                //.contact(contact)
                .version("1.0")
                .build();
    }
}
